package com.cps.match.service.impl;

import java.io.IOException;

import org.apache.log4j.Logger;

import com.cps.match.connection.ConnectionHelper;
import com.cps.match.connection.MatchClientRequester;
import com.cps.match.sdo.AcquirerId;
import com.cps.match.sdo.EnquiryRequest;
import com.cps.match.sdo.EnquiryResponse;
import com.cps.match.sdo.Offset;
import com.cps.match.service.MatchEnquiryService;
import com.cps.match.xmlparser.EnquiryRequestParser;
import com.cps.match.xmlparser.EnquiryResponseParser;

public class MatchEnquiryServiceImpl implements MatchEnquiryService{
	protected static final Logger log = Logger.getLogger(ConnectionHelper.class);
	
	private String endpoint;
	private String applicationId;
	
	public MatchEnquiryServiceImpl(String applicationId, String endpoint) {
		this.endpoint = endpoint;
	}
	@Override
	public EnquiryResponse enquire(EnquiryRequest enquiryRequest) throws IOException {
		String xmlRequest = EnquiryRequestParser.marshall(enquiryRequest);
		log.debug("REQUEST"+xmlRequest);
		
		String requestEndpoint = getUrlRequest(enquiryRequest.getOffset());
		log.debug("ENDPOINT="+requestEndpoint);
		
		MatchClientRequester matchServiceClient = new MatchClientRequester();
		String content = matchServiceClient.enquire(requestEndpoint, xmlRequest);
		log.debug("RESPONSE="+content);
		EnquiryResponse enquiryResponse = EnquiryResponseParser.unmarshall(content);
		enquiryResponse.setReferenceId(getRefId(enquiryResponse.getReference()));
		enquiryResponse.setApplicationId(applicationId);
		return enquiryResponse;
	}
	
	@Override
	public EnquiryResponse enquireHistory(EnquiryRequest enquiryRequest) throws IOException {
		String requestEndpoint = getUrlRequestHistory(enquiryRequest.getRefId(), enquiryRequest.getAcquirerId(), enquiryRequest.getOffset());
		log.debug("ENDPOINT="+requestEndpoint);
		
		MatchClientRequester matchServiceClient = new MatchClientRequester();
		String content = matchServiceClient.enquire(requestEndpoint, null);
		log.debug("RESPONSE="+content);
		EnquiryResponse enquiryResponse = EnquiryResponseParser.unmarshall(content);
		enquiryResponse.setReferenceId(getRefId(enquiryResponse.getReference()));
		enquiryResponse.setApplicationId(applicationId);
		return enquiryResponse;
	}
	
	//----------------------PRIVATE---------------------------------------------
	
	private String encodeOffsetParam(Offset offset) {
		String template ="Format=XML&PageOffset=%s&PageLength=%s";
		return String.format(template, offset.getPageOffset(), offset.getTotalLength());
	}
	
	private String getUrlRequest(Offset offset) {
		return endpoint+"?"+encodeOffsetParam(offset);
	}
	
	private String getUrlRequestHistory(String refId, AcquirerId acquirerId, Offset offset) {
		return endpoint+"/"+ refId+"?AcquirerId="+acquirerId.getId()+"&"+encodeOffsetParam(offset);
	}

	private String getRefId(String ref) {
		if(ref==null) {
			return ref;
		}
		int indx = ref.lastIndexOf("/");
		return ref.substring(indx+1, ref.length());
	}
}
